package www.openteam.com.ui.realtime;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.File;
import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import www.openteam.com.R;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.view.SignatureView;

/**
 * Created by kylin on 2016/8/4.
 */
public class SignatureFragment extends Fragment {

    @Bind(R.id.signatureView)
    SignatureView signatureView;
    @Bind(R.id.clear)
    AppCompatButton clear;
    @Bind(R.id.retrate)
    AppCompatButton retrate;
    @Bind(R.id.advance)
    AppCompatButton advance;
    @Bind(R.id.save)
    AppCompatButton save;
    private String mParam;
    private String TAG = getClass().getSimpleName();


    public SignatureFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam = getArguments().getString("key");
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_signature, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.clear, R.id.retrate, R.id.advance, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear:
                signatureView.clear();
                break;
            case R.id.retrate:
                signatureView.goBack();
                break;
            case R.id.advance:
                signatureView.goForward();
                break;
            case R.id.save:
//                String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy";
//                Log.e(TAG, "onViewClicked: "+storePath);/
                try {
                    final File file=signatureView.save("");
                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri uri = Uri.fromFile(file);
                    intent.setData(uri);
                    getContext().sendBroadcast(intent);//这个广播的目的就是更新图库，发了这个广播进入相册就可以找到你保存的图片了！，记得要传你更新的file哦
                    ToastUtil.showToast(getContext(),"提交成功");
                    signatureView.clear();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                Bitmap bitMap = signatureView.getBitMap();
                break;
        }
    }



}
